﻿using System.Text.Encodings.Web;
using System.Text.Json;

namespace GameView.Comment
{
    public class JSON
    {
        private static JsonSerializerOptions options = new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase, // 使用camelCase命名策略
            WriteIndented = true, // 可选：格式化输出
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        };

        public static string Serialize<T>(T obj)
        {
            return JsonSerializer.Serialize(obj, options);
        }

        public static T Deserialize<T>(string jsonStr)
        {
            try
            {
                return JsonSerializer.Deserialize<T>(jsonStr, options);
            }
            catch (Exception)
            {
                return default;
            }
        }
    }
}